/*****************************************************************************
 *   Copyright (C) 2008 John Schember <john@nachtimwald.com>                 *
 *                                                                           *
 *   This file is part of niwbillmanager.                                    *
 *                                                                           *
 *   niwbillmanager is free software: you can redistribute it and/or         *
 *   modify it under the terms of the GNU General Public License as          *
 *   published by the Free Software Foundation, either version 3 of the      *
 *   License, or (at your option) any later version.                         *
 *                                                                           *
 *   niwbillmanager is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            *
 *   GNU General Public License for more details.                            *
 *                                                                           *
 *   You should have received a copy of the GNU General Public License       *
 *   along with niwbillmanager. If not, see                                  *
 *   <http://www.gnu.org/licenses/>.                                         *
 *****************************************************************************/

#include <billitem.h>

#include "billitemdialog.h"

BillItemDialog::BillItemDialog(const BillItem &billItem, QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);

    paymentRecurringPeriodCombo->addItem(tr("None"),
        RecurringPeriodType::NONE);
    paymentRecurringPeriodCombo->addItem(tr("Monthly"),
        RecurringPeriodType::MONTHLY);
    paymentRecurringPeriodCombo->addItem(tr("Last day of the month"),
        RecurringPeriodType::LASTDAYOFMONTH);
    paymentRecurringPeriodCombo->addItem(tr("Yearly"),
        RecurringPeriodType::YEARLY);

    m_id = billItem.getId();

    nameEdit->setText(billItem.getName());
    amountDueSpin->setValue(billItem.getAmountDue());
    payeeEdit->setText(billItem.getPayee());
    paymentLocationEdit->setText(billItem.getPaymentLocation());
    paymentMethodEdit->setText(billItem.getPaymentMethod());

    int recurringPeriodIndex = paymentRecurringPeriodCombo->findData(
        billItem.getPaymentRecurringPeriod());
    if (recurringPeriodIndex != -1) {
        paymentRecurringPeriodCombo->setCurrentIndex(recurringPeriodIndex);
    }

    dateDueCalendar->setSelectedDate(billItem.getDateDue());
    notesText->setText(billItem.getNotes());
    tagsEdit->setText(BillItem::stringSetToString(billItem.getTags()));
    paidCheck->setChecked(billItem.getPaid());
    autoPayCheck->setChecked(billItem.getAutoPay());
}

BillItem BillItemDialog::getBillItem()
{
    BillItem billItem;

    billItem.setId(m_id);

    billItem.setName(nameEdit->text());
    billItem.setAmountDue(amountDueSpin->value());
    billItem.setPayee(payeeEdit->text());
    billItem.setPaymentLocation(paymentLocationEdit->text());
    billItem.setPaymentMethod(paymentMethodEdit->text());
    // TODO: Don't use a cast from int.
    billItem.setPaymentRecurringPeriod(
        (RecurringPeriodType::RecurringPeriodTypes)
        (paymentRecurringPeriodCombo->itemData(
        paymentRecurringPeriodCombo->currentIndex())
        .toInt()));
    billItem.setDateDue(dateDueCalendar->selectedDate());
    billItem.setNotes(notesText->toPlainText());
    billItem.setTags(tagsEdit->text().simplified().split(" ").toSet());
    billItem.setPaid(paidCheck->isChecked());
    billItem.setAutoPay(autoPayCheck->isChecked());
    return billItem;
}

void BillItemDialog::setCurrencySymbol(QString symbol)
{
    amountDueSpin->setPrefix(symbol);
}
